package com.example.dialog.Activity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.dialog.R;
import com.example.dialog.Tools.SendEmail;

import java.util.Random;

public class RegisterActivity extends AppCompatActivity {

    private EditText et_account;
    private EditText et_password;
    private EditText et_confirm_password;
    private EditText et_username;
    private EditText et_verification_code;
    private Button btn_register;
    private Button btn_back_login;
    private Button btn_get_verification_code;

    private String verificationCode;  // 用于保存生成的验证码
    private String userAccount;  // 用户输入的邮箱

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        this.et_account = findViewById(R.id.et_account);
        this.et_password = findViewById(R.id.et_password);
        this.et_confirm_password = findViewById(R.id.et_confirm_password);
        this.et_username = findViewById(R.id.et_username);
        this.et_verification_code = findViewById(R.id.et_verification_code);
        this.btn_register = findViewById(R.id.btn_register);
        this.btn_back_login = findViewById(R.id.btn_back_login);
        this.btn_get_verification_code = findViewById(R.id.btn_get_verification_code);

        btn_get_verification_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userAccount = et_account.getText().toString().trim();
                if (userAccount.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请输入邮箱地址", Toast.LENGTH_SHORT).show();
                } else {
                    verificationCode = generateVerificationCode();  // 生成验证码
                    SendEmail.sendMail(userAccount, "注册验证码", "您的验证码是: " + verificationCode);
                    Toast.makeText(RegisterActivity.this, "验证码已发送", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                check();
            }
        });

        btn_back_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void check() {
        String et_account = this.et_account.getText().toString().trim();
        String et_password = this.et_password.getText().toString().trim();
        String et_confirm_password = this.et_confirm_password.getText().toString().trim();
        String et_username = this.et_username.getText().toString().trim();
        String enteredVerificationCode = this.et_verification_code.getText().toString().trim();

        if (et_account.isEmpty() || et_password.isEmpty()) {
            Toast.makeText(RegisterActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
        } else if (et_confirm_password.isEmpty()) {
            Toast.makeText(RegisterActivity.this, "请确认密码", Toast.LENGTH_SHORT).show();
        } else if (!et_password.equals(et_confirm_password)) {
            Toast.makeText(RegisterActivity.this, "确认密码与输入密码不一致", Toast.LENGTH_SHORT).show();
        } else if (et_username.isEmpty()) {
            Toast.makeText(RegisterActivity.this, "请输入昵称", Toast.LENGTH_SHORT).show();
        } else if (et_username.length() > 7) {
            Toast.makeText(RegisterActivity.this, "昵称长度非法！", Toast.LENGTH_SHORT).show();
        } else if (!enteredVerificationCode.equals(verificationCode)) {
            Toast.makeText(RegisterActivity.this, "验证码不正确", Toast.LENGTH_SHORT).show();
        } else {
            if (new DatabaseHelperForRegister(this).check_usr(et_account)) {
                Toast.makeText(RegisterActivity.this, "此账号已被注册！", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(RegisterActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                new DatabaseHelperForRegister(this).insertNewUsr(et_account, et_password, et_username);
            }
        }
    }

    // 生成 4 位随机验证码
    private String generateVerificationCode() {
        Random random = new Random();
        int code = random.nextInt(9000) + 1000;  // 生成一个 4 位数字
        return String.valueOf(code);
    }

    class DatabaseHelperForRegister extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "searchdatabase.db";
        private static final int DATABASE_VERSION = 1;
        private static final String TABLE_NAME = "usrinfo";
        private static final String COL_1 = "usrAct";
        private static final String COL_2 = "usrPwd";
        private static final String COL_3 = "usrName";

        public DatabaseHelperForRegister(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        }

        public void insertNewUsr(String usrAct, String usrPwd, String usrName) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(COL_1, usrAct);
            values.put(COL_2, usrPwd);
            values.put(COL_3, usrName);
            db.insert(TABLE_NAME, null, values);
            db.close();
        }

        public boolean check_usr(String usrAct) {
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.query(TABLE_NAME, null, COL_1 + "=?",
                    new String[]{usrAct}, null, null, null);

            boolean exists = cursor.getCount() > 0;
            cursor.close();
            db.close();
            return exists;
        }
    }
}
